<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../libs/vue.js"></script>
  <style>
    .static{
      width:120px;
      height:120px;
      margin:20px;
      text-align: center;
      line-height: 120px;
      font-size: 50px;
    }
    .a{
      background: crimson;
    }
    .b{
      border-radius: 50%;
    }
    .c{
      border:4px solid darkviolet;
      box-shadow:  0px 0px 2px 2px yellow;
    }
    .nav{
      display: flex;
    }
    .nav-item{
      width:100px;
      height:40px;
      line-height: 40px;
      text-align: center;
      background: deepskyblue;
      border-radius: 8px;
      color:#fff;
      font-size: 14px;
      margin:0 10px;
      cursor: pointer;
    }
    .nav-active{
      background: brown;
      color:deepskyblue;
    }
    .main{
      width:360px;
      padding:0 10px;
      margin-top:20px;
      box-sizing: border-box;
    }
    .content{
      width:100%;
      height:340px;
      background: brown;
      border-radius: 10px;
      text-align: center;
      line-height: 340px;
      font-size: 40px;
      color:#fff;
    }
    td{
      border:1px solid #999;

    }
  </style>
</head>
<body>
  <div id="app">
    <h2>{{title }}</h2>
    <table>
      <thead>
        <tr>
          <td>序号</td>
          <td>选中</td>
          <td>名称</td>
          <td>描述</td>
          <td>单价</td>
          <td>数量</td>
          <td>小计(元)</td>
          <td>操作</td>
        </tr>
      </thead>
      <tbody >
         <template v-if="goods.length>0">
          <tr v-for="(g,i) in goods" :key="i">
              <td>{{i+1}}</td>
              <td> <input type="checkbox" v-model="g.check" ></td>
              <td> <b> {{g.title }}</b></td>
              <td> <img width="100" height="50" :src="imgSrc" alt=""> </td>
              <td> <input type="text"  min="1" v-model.number="g.price" ></td>
              <td> <input type="number" min="1" v-model.number="g.count" ></td>
              <td> {{g.price * g.count}}</td>
              <td> <button @click="goods.splice(i,1)">删除</button></td>
          </tr>
         </template>
         <tr v-else >
            <td colspan="8">
              暂无数据,请等候
            </td>
         </tr>
      </tbody>
    </table>

    <div>
      <p>
        <label for="quan">
          <input type="checkbox" id="quan" v-model="quan"> 全选反选
        </label>
        <button @click="delCheck"> 删除选中(批量删除)</button>
      </p>
      <p>总数量:  {{total}}  - {{newCount}} </p>
      <p>选中数量: {{checkTotal}}  = {{checkCount}} </p>
      <p>总价值:  {{amount}} </p>
      <p>选中价值: {{chechAmount}}  </p>
      <button>结算</button>
    </div>
  </div>
  
  <script>

    const vm = new Vue({

      data:{
        title:"Vue 快速实现购物车  " ,
        goods:[
            {check:false,title:"上衣",price:200,count:10},
            {check:true,title:"裤子",price:100,count:10},
            {check:false,title:"鞋子",price:500,count:10},
            {check:false,title:"风衣",price:1000,count:10},
            {check:true,title:"帽子",price:300,count:10},
            {check:true,title:"西服",price:2000,count:10},
        ],
        imgSrc:'http://47.104.209.44/base/imgs/1.jpg',
        total:0,
        checkTotal:0,
        amount:0,
        chechAmount:0
      },
      computed:{
        quan:{
          get(){  // 取值
            // var flag = true;
            // this.goods.forEach(item=>{
            //     if(!item.check) flag = false;
            // })
            // return flag;

            // every
            return this.goods.every(item=>item.check)
          },
          set(v){
              // 监听主动修改
              // console.log(v)
              // this.goods = this.goods.map(item=>{
              //   item.check = v;
              //   return item 
              //   // return {...item,check:v}
              // })

              this.goods.forEach((item,index)=>{
                this.$set(this.goods,index,{...item,check:v})
              })
          }
        },

        newCount(){
          var count = 0
          this.goods.forEach(item=>{
             count+=item.count
          })
          return count;
        },
        checkCount(){
          var count = 0
          this.goods.forEach(item=>{
             count+=item.check?item.count:0
          })
          return count;
        }
      },
      watch:{
        // 深度监听  
        goods:{
          deep:true,
          handler(v){
            console.log(v)
            this.total = 0
            this.amount = 0
            this.checkTotal = 0
            this.chechAmount = 0
            v.forEach(item=>{
              this.total += item.count
              this.amount += item.price * item.count
              this.checkTotal += item.check?item.count:0;
              this.chechAmount += item.check?item.count*item.price:0;
            })
          }
        }
        // goods:function(v){
        //   console.log(v)
        // }
      },
      methods:{
        delCheck(){
          // 选中true删除  filter true 是要保留下来  
          this.goods = this.goods.filter(item=>!item.check);
        }
      },
      mounted(){
        document.querySelectorAll("title")[0].innerHTML = this.title; 
        this.goods.forEach(item=>{
           this.total += item.count
           this.amount += item.price * item.count
           this.checkTotal += item.check?item.count:0;
           this.chechAmount += item.check?item.count*item.price:0;
        })
      }
    }).$mount("#app")
    var user = {
      age:18,
      names:'zuozuomu'
    }


  </script>

</body>
</html>